package com.dev.module_zqc_sleep.mvi.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.dev.module_ui_composable.coil.LoadAsyncImage
import com.dev.module_ui_composable.view.ad.BannerAdView
import com.dev.module_zqc_sleep.R
import com.dev.module_zqc_sleep.mvi.vm.AssetsViewModel

@Composable
fun SleepHomePage(
    assetsViewModel: AssetsViewModel = viewModel(),
    navigateToMusic: (Int) -> Unit
) {
    val context = LocalContext.current
    val assetImages by assetsViewModel.assetImages.collectAsStateWithLifecycle()
    val randomFolderIndex = remember {
        derivedStateOf { (4..6).random() }
    }

    LaunchedEffect(true) {
        assetsViewModel.loadAssetImages(context, randomFolderIndex.value)
    }

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        contentWindowInsets = WindowInsets(0, 0, 0, 0)
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .verticalScroll(rememberScrollState())
        ) {
            Box(modifier = Modifier.fillMaxWidth()) {
                Image(
                    painter = painterResource(R.mipmap.module_sleep_img1),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight(),
                    contentScale = ContentScale.FillWidth
                )

                Text(
                    text = "睡眠",
                    modifier = Modifier
                        .statusBarsPadding()
                        .padding(start = 35.dp, top = 15.dp),
                    color = Color.White,
                    fontSize = 20.sp
                )
            }

            MusicWithButtons(navigateToMusic)
            BannerAdView()

            MusicContent(
                assetImages = assetImages.take(2),
                title = "自然音",
                onNavigateMusic = { navigateToMusic(4) }
            )

            MusicContent(
                assetImages = assetImages.drop(2).take(2),
                title = "助眠脑波",
                onNavigateMusic = { navigateToMusic(5) }
            )

            MusicContent(
                assetImages = assetImages.drop(4).take(2),
                title = "冥想",
                onNavigateMusic = { navigateToMusic(6) }
            )
        }
    }
}

@Composable
private fun MusicWithButtons(
    onNavigateMusic: (position: Int) -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(80.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
                .clickable { onNavigateMusic(0) },
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically)
        ) {
            Image(
                painter = painterResource(R.mipmap.module_sleep_ic1),
                contentDescription = null,
                modifier = Modifier.weight(1f)
            )

            Text(text = "助眠")
        }


        Column(
            modifier = Modifier
                .weight(1f)
                .clickable { onNavigateMusic(1) },
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically)
        ) {
            Image(
                painter = painterResource(R.mipmap.module_sleep_ic2),
                contentDescription = null,
                modifier = Modifier.weight(1f)
            )

            Text(text = "治愈")
        }

        Column(
            modifier = Modifier
                .weight(1f)
                .clickable { onNavigateMusic(2) },
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically)
        ) {
            Image(
                painter = painterResource(R.mipmap.module_sleep_ic3),
                contentDescription = null,
                modifier = Modifier.weight(1f)
            )

            Text(text = "静心")
        }

        Column(
            modifier = Modifier
                .weight(1f)
                .clickable { onNavigateMusic(3) },
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically)
        ) {
            Image(
                painter = painterResource(R.mipmap.module_sleep_ic4),
                contentDescription = null,
                modifier = Modifier.weight(1f)
            )

            Text(text = "白噪音")
        }
    }
}

@Composable
private fun MusicContent(
    assetImages: List<String>,
    title: String,
    onNavigateMusic: () -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 20.dp),
        verticalArrangement = Arrangement.spacedBy(20.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 30.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                text = title,
                fontSize = 20.sp
            )
            
            TextButton(onClick = { onNavigateMusic() }) {
                Text(text = "更多>")
            }
        }

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(15.dp, Alignment.CenterHorizontally)
        ) {
            assetImages.forEach { imagePath ->
                LoadAsyncImage(
                    data = "file:///android_asset/$imagePath",
                    modifier = Modifier
                        .height(160.dp)
                        .weight(1f)
                        .clickable { onNavigateMusic() },
                    contentScale = ContentScale.FillBounds
                )
            }
        }
    }
}